package com.montran.search;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import com.exceptions.FilterException;
import com.exceptions.ResourcesException;
import com.montran.read.resources.UtilPropertyFiles;

public class SearchWord implements Search {
	private List<String> words;
	private String stringSource;
	private Integer operation;

	public SearchWord(String nameResource, Integer operation) {
		words = new ArrayList<String>();
		this.operation = operation;
		fillData(nameResource);
	}

	public SearchWord(Integer operation, String... stringToFind) {
		words = new ArrayList<String>();
		this.operation = operation;
		words.addAll(Arrays.asList(stringToFind));
	}

	@Override
	public Boolean find() throws FilterException {
		Boolean result = false;
		if (operation == 1 && words.contains(stringSource)) {
			result = true;
		}
		if (operation == 2) {
			for (String word : words) {
				if (stringSource.indexOf(word) != -1) {
					result = true;
					break;
				}
			}
		}
		return result;
	}

	@Override
	public void fillData(String nameResource) {
		try {
			String resourceWords = new String();
			resourceWords = UtilPropertyFiles.readPropertiesFile(nameResource);
			words = Arrays.asList(resourceWords.split(","));
		} catch (ResourcesException e) {
			e.toString();
		}
	}
	
	@Override
	public void setDataSource(Object data) {
		this.stringSource = (String) data;
	}

}
